/*
 ============================================================================
 Name		: Client.h
 Author	  : wangli@opi-corp.com
 Copyright   : Your copyright notice
 Description : ClientSession
 ============================================================================
 */

#ifndef __CLIENT_H__
#define __CLIENT_H__

#include <e32base.h>
#include "RCallStack.h"

class ClientSession : public RSessionBase
{
public:
    ClientSession();
    ~ClientSession();
    
    void ConnectL();
    void CreateCallStack(const TDesC& aStackName, TInt aStackSize = 4096);
    void ClientLeave();
    void Close();
    
    TVersion Version()const;
    
    inline RCallStack& Stack()
    {
        return iStack;
    }
   
private:
    RCallStack iStack;
};

class CClient;
class TClientEntry
{
public:
    TFullName iClientName;
    CClient* iInstance;
    
    TClientEntry()
        :iInstance(NULL)
    {
       
    }
      
    ~TClientEntry();  
    
//    TClientEntry& operator=(const TClientEntry& aClientEntry)
//    {
//        if(this == &aClientEntry)
//            return *this;
//        
//        this->iClientName.SetLength(0);
//        this->iClientName.Append(aClientEntry.iClientName);
//        this->iInstance = aClientEntry.iInstance;
//        
//        return *this;
//    }
//    
//    TBool operator==(const TClientEntry& aClientEntry)const
//    {
//        if(this == &aClientEntry)
//            return ETrue;
//        
//        if(this->iClientName == aClientEntry.iClientName)
//            return ETrue;
//        return EFalse;
//    }
};

/*
 *  当CClient无法容纳查过KMaxClientNumber数量时，InstanceL将抛出此错误
 */
#define KErrFull 10 
class CClient : public CBase
{
public:
    
    ~CClient();
    enum
    {
        KMaxClientNumber = 4
    };
    
    static CClient* InstanceL(const TDesC& aClientName, TInt aStackSize);
    static void FreeAllInstance();
    static void CloseInstance(const TDesC& aClientName);
    
    inline RCallStack& Stack()
    {
        return iSession.Stack();
    }
    
    /**
     * 以下API属于Test API, 不应该在源代码中使用
     */
    static TInt Count()
    {
        return gCurrentCount;  
    }
    
    static CClient* GetIndex(TInt index)
    {
        return gInstanceMap[index].iInstance;
    }
    ///========end==============
private:
    static CClient* NewL(const TDesC& aClientName, TInt aStackSize);
    void ConstuctL(const TDesC& aClientName, TInt aStackSize);
    CClient();
 
private:
    static TClientEntry gInstanceMap[KMaxClientNumber];
    static TInt gCurrentCount;
    
    friend class TTracer;
    ClientSession iSession;
};



#endif  // __CLIENT_H__

